package com.funo.wcity.app.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.funo.wcity.app.entity.LoginUser;
import com.funo.wcity.app.entity.TblMailDetail;
import com.funo.wcity.app.service.LoginUserService;
import com.funo.wcity.app.service.TblCompaniesService;
import com.funo.wcity.app.service.TblCompanyTypesService;
import com.funo.wcity.app.service.TblContactPersonsService;
import com.funo.wcity.app.service.TblMailDetailService;
import com.funo.wcity.app.service.TblemployeesService;
import com.funo.wcity.app.utils.Constant;
import com.funo.wcity.app.utils.JsonUtil;
import com.funo.wcity.app.utils.MD5;

@Controller
@SuppressWarnings("all")
public class LoginController extends CommonController{
	@Resource(name="loginUserService")
	private LoginUserService loginUserService;
	@Resource(name="tblemployeesService")
	private TblemployeesService tblemployeesService;
	@Resource(name="tblContactPersonsService")
	private TblContactPersonsService tblContactPersonsService;
	@Resource(name="tblCompaniesService")
	private TblCompaniesService tblCompaniesService;
	@Resource(name="tblCompanyTypesService")
	private TblCompanyTypesService tblCompanyTypesService;
	@Resource(name="tblMailDetailService")
	private TblMailDetailService tblMailDetailService;
	/**
	 * 验证登入是否成功
	 * return 
	 * 		成功：继续；失败：返回首页
	 */
	@RequestMapping("/login")
	public String login(HttpServletRequest request,HttpServletResponse response,String username,String password){
		LoginUser loginUser=this.loginUserService.findLoginUser(username,MD5.EncryptData(password));
		//不为空登入成功
		if(loginUser!=null){
			//当前操作用户名
			request.getSession().setAttribute(Constant.LOGIN_USER, loginUser);
			//查询所有员工id
			request.getSession().setAttribute(Constant.LOGIN_EMPLOYEE,this.tblemployeesService.getTblemployeesIdStr(loginUser.getIdEmployee()+""));
		    JsonUtil.stringPrintJson(request, response, "true");
			return  null;
		}else{
			JsonUtil.stringPrintJson(request, response, "false");
			return null;
		}
	}
	
	/**
	 * 退出
	 */
	@RequestMapping("/quit")
	public String quit(HttpServletRequest request,HttpServletResponse response){
		request.getSession().removeAttribute(Constant.LOGIN_USER);
		return "web/login";
	}
}
